#include "StdAfx.h"
#include "OrganEnvelope.h"

COrganEnvelope::COrganEnvelope(void)
{
	m_attack = 0.05;
	m_decay = 0.05;
    m_release = 0.01;
    m_duration = 0.1;
    m_original = NULL;
}

COrganEnvelope::~COrganEnvelope(void)
{
}



void COrganEnvelope::Start()
{
    m_time = 0;
}

bool COrganEnvelope::Generate()
{
   
    double gain = 1;
	double StartRelease;
    if(m_time < m_attack)
    {
        gain = 2 * (m_time / m_attack);
    }
	else if(m_time > m_attack && m_time < (m_attack + m_decay))
	{
		gain = 2 - ((m_time - m_attack) / m_decay);
	}
    else if(m_time > (m_duration - m_release) && m_time < m_duration)
    {
        StartRelease = m_duration - m_release;
        gain = 1 - (m_time - StartRelease) / (m_duration - StartRelease);
    }
    else if(m_time >= m_duration)
    {
        m_frame[0] = 0;
		m_frame[1] = 0;
        return false;
    }

    if(m_original != NULL)
    {
        m_frame[0] = m_original->Frame(0) * gain;
        m_frame[1] = m_original->Frame(1) * gain;
    }

    m_time += GetSamplePeriod();
    return true;
}
